﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace 原型模式
{
    /// <summary>
    /// 原型类
    /// </summary>
    [Serializable]
    public class StudentPrototype
    {

        private StudentPrototype()
        {
            Thread.Sleep(2000);
            long lResult = 0;
            for (int i = 0; i < 100000000; i++)
            {
                lResult += i;
            }
            Console.WriteLine("{0}被构造..", this.GetType().Name);
        }

        public int Id { get; set; }
        public string Name { get; set; }
        public Class Class { get; set; }
        public void Study()
        {
            Console.WriteLine("{0}在学习.net高级班公开课之设计模式特训", this.Name);
        }

        private static StudentPrototype _StudentPrototype = null;
        static StudentPrototype()
        {
            _StudentPrototype = new StudentPrototype()
            {
                Id = 88888,
                Name = "大黄瓜",
                Class= new Class {
                 Num=1,
                 Remark= "大黄瓜1212"
                }
            };
        }

        public static StudentPrototype CreateInstance()
        {
           return (StudentPrototype)_StudentPrototype.MemberwiseClone(); //克隆一对象
        }

        public static StudentPrototype CreateInstanceSerialize()
        {
            return SerializeHelper.DeepClone<StudentPrototype>(_StudentPrototype);
        }

    }

    /// <summary>
    /// 班级
    /// </summary>
    [Serializable]
    public class Class
    {
        public int Num { get; set; }
        public string Remark { get; set; }
    }
}
